package org.hhchat.leetcode.array;

/*
 * https://leetcode.com/problems/minimum-path-sum/description/
 */
public class code64 {
    public static class Solution {
        public int minPathSum(int[][] grid) {
            int xlen = grid.length;
            int ylen = grid[0].length;
            int dp[][] = new int[xlen][ylen];
            dp[0][0]=grid[0][0];
            for(int i=1;i < ylen;i++) {
                dp[0][i] = dp[0][i - 1] + grid[0][i];
            }
            for(int i=1;i< xlen;i++) {
                dp[i][0] = dp[i - 1][0] + grid[i][0];
            }
            for(int i=1;i<xlen;i++) {
                for(int j=1;j<ylen;j++) {
                    dp[i][j]=(dp[i-1][j]>dp[i][j-1]?dp[i][j-1]:dp[i-1][j])+grid[i][j];
                }
            }
            return dp[xlen - 1][ylen - 1];
        }
    }

    public static void main(String[] args){
//        int[][] grid = new int[5][5];
//        for(int i=0;i<5;i++) {
//            for(int j=0;j<5;j++) {
//                grid[i][j] = j;
//            }
//        }
        int[][] grid = {{1,2},{1,1}};
        System.out.println(new Solution().minPathSum(grid));
    }
}
